/**
 * @file boards\bsp_htr3218.c
 *
 * Copyright (C) 2023
 *
 * bsp_key1.c is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author HinsShum hinsshum@qq.com
 *
 * @encoding utf-8
 */

/*---------- includes ----------*/
#include "cpu.h"
#include "device.h"
#include "gd32f30x_gpio.h"
#include "htr32xx.h"
#include "options.h"

/*---------- macro ----------*/
#define HTR3236_SDB1_RCU_GPIOX (RCU_GPIOB)
#define HTR3236_SDB1_PORT (GPIOB)
#define HTR3236_SDB1_PIN (GPIO_PIN_5)
#define HTR3236_EN_RCU_GPIOX (RCU_GPIOB)
#define HTR3236_EN_PORT (GPIOB)
#define HTR3236_EN_PIN (GPIO_PIN_4)
/*---------- type define ----------*/
/*---------- variable prototype ----------*/
/*---------- function prototype ----------*/
static bool bsp_init(void);
static void bsp_deinit(void);
static void bsp_htr3218_enable(void);
static void bsp_htr3218_disable(void);

/*---------- variable ----------*/
static htr32xx_describe htr3218 = {
                                    .htr32xx_iic_name = "htr_iic",
                                    .htr32xx_dev_name = HTR3218_NAME,
                                    .htr32xx_ledx_max = 18,
                                    .bus = NULL,
                                    .address = 0x78,
                                    .mem_addr_counts = 1,
                                    .init = bsp_init,
                                    .deinit = bsp_deinit,
                                    .bsp_chip_enable = bsp_htr3218_enable,
                                    .bsp_chip_disable = bsp_htr3218_disable };
DEVICE_DEFINED(htr3218, htr32xx, &htr3218);

/*---------- function ----------*/
static bool bsp_init(void) {
    rcu_periph_clock_enable(HTR3236_SDB1_RCU_GPIOX);
    rcu_periph_clock_enable(HTR3236_EN_RCU_GPIOX);
    rcu_periph_clock_enable(RCU_AF);
    gpio_pin_remap_config(GPIO_SWJ_NONJTRST_REMAP, ENABLE);
    gpio_init(HTR3236_SDB1_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, HTR3236_SDB1_PIN);
    gpio_init(HTR3236_EN_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, HTR3236_EN_PIN);
    gpio_bit_set(HTR3236_SDB1_PORT, HTR3236_SDB1_PIN);
    gpio_bit_reset(HTR3236_EN_PORT, HTR3236_EN_PIN);
    return true;
}
static void bsp_deinit(void) {
    rcu_periph_clock_enable(HTR3236_SDB1_RCU_GPIOX);
    rcu_periph_clock_enable(HTR3236_EN_RCU_GPIOX);
    rcu_periph_clock_enable(RCU_AF);
    gpio_init(HTR3236_SDB1_PORT, GPIO_MODE_OUT_OD, GPIO_OSPEED_2MHZ, HTR3236_SDB1_PIN);
    gpio_init(HTR3236_EN_PORT, GPIO_MODE_OUT_OD, GPIO_OSPEED_2MHZ, HTR3236_EN_PIN);
    gpio_bit_reset(HTR3236_SDB1_PORT, HTR3236_SDB1_PIN);
    gpio_bit_set(HTR3236_EN_PORT, HTR3236_EN_PIN);
}
static void bsp_htr3218_enable(void) {
    gpio_bit_set(HTR3236_SDB1_PORT, HTR3236_SDB1_PIN);
    gpio_bit_reset(HTR3236_EN_PORT, HTR3236_EN_PIN);
}
static void bsp_htr3218_disable(void) {
    gpio_bit_reset(HTR3236_SDB1_PORT, HTR3236_SDB1_PIN);    
    gpio_bit_set(HTR3236_EN_PORT, HTR3236_EN_PIN);
}